package com.hyxt.DTO.process;

import com.hyxt.DTO.CommonMessage;
import com.hyxt.DTO.CommonMessageHeader;
import com.hyxt.DTO.T809_MessageHeader;
import com.hyxt.superviseservice.CommonMessageBody;
import com.hyxt.superviseservice.IMessageBody;
import com.hyxt.superviseservice.messageBody.T809_0x1206;
import com.hyxt.utils.BytesUtil;
import com.hyxt.utils.T809_Process;
import com.hyxt.utils.T809_Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @Project: CNPC_VMS
 * @Title: T809_0x1206_Process
 * @Description: 
 * @author: denghl
 * @date: 2014年4月24日 下午5:11:36
 * @company: Beijing Huayou Information andCommunication Technology Co.,Ltd
 * @Copyright: Copyright (c) 2014
 * @version V2.0
 */
public class T809_0x1206_Process<B extends IMessageBody> extends T809_Process<B> {
	private static Log log = LogFactory.getLog(T809_0x1206_Process.class);
	@Override
	protected CommonMessageBody getBody(CommonMessageHeader header, byte[] data) {
		// TODO Auto-generated method stub
		T809_0x1206 body = new T809_0x1206();
		//车牌号
		byte[] VEHICLE_NO = BytesUtil.cutBytes(0, 21, data);
		//车牌颜色
		byte VEHICLE_COLOR = BytesUtil.getByte(21, data);
		byte[] DATA_TYPE = BytesUtil.getWord(22, data);
		byte[] DATA_LENGTH = BytesUtil.cutBytes(24, 4, data);
	try {
		//设置车牌号
			body.setVEHICLE_NO(new String(VEHICLE_NO,"GBK"));
		} catch (UnsupportedEncodingException e) {
			log.warn("",e);
		}
	//设置车牌颜色
		body.setVEHICLE_COLOR(VEHICLE_COLOR);
		body.setDATA_TYPE(BytesUtil.parseBytesToShort(DATA_TYPE));
		body.setDATA_LENGTH(BytesUtil.parseBytesToInt(DATA_LENGTH));
		return body;
	}
	/**
	 * @Description:数据打包类
	 */
	@Override
	protected byte[] packData(CommonMessage<B> message) {
		T809_0x1206 body1206 = (T809_0x1206) message.getMessageBody();
		// 此数据体数据长度为27,数据长度(包括头标识、数据头、数据体、CRC校验码和尾标识)
		// 总共68字节
		byte[] head = new byte[22];
		byte[] datalength = BytesUtil.int2bytes4(54);
		datalength = BytesUtil.getBigDWord(0, datalength);
		System.arraycopy(datalength, 0, head, 0, datalength.length);

		// 报文序列号*占4字节
		byte[] MSG_SN = BytesUtil.int2bytes4(((T809_MessageHeader) message.getMessageHeader()).getSn());
		head[4] = MSG_SN[3];
		head[5] = MSG_SN[2];
		head[6] = MSG_SN[1];
		head[7] = MSG_SN[0];
		// 业务数据类型,占2字节
		// byte[] MSG_ID =
		// BytesUtil.int2bytes2(Integer.parseInt((((T809_MessageHeader)message.getMessageHeader()).getMessageID()).toString()));
		head[8] = (byte) 0x12;
		head[9] = 0x00;
		// 下级平台接入码，上级平台给下级平台分配唯一标识码。占4字节
		byte[] MSG_GNSSCENTERID = BytesUtil.int2bytes4(((T809_MessageHeader) message.getMessageHeader()).getGnsscentrid());
		MSG_GNSSCENTERID = BytesUtil.getBigDWord(0, MSG_GNSSCENTERID);
		System.arraycopy(MSG_GNSSCENTERID, 0, head, 10, MSG_GNSSCENTERID.length);
		// 协议版本好标识，上下级平台之间采用的标准协议版编号；长度为3个字节来表示，0x01 0x02 0x0F
		// 标识的版本号是v1.2.15，以此类推。
		byte[] VERSION_FLAG = ((T809_MessageHeader) message.getMessageHeader()).getVersonFlag();
		System.arraycopy(VERSION_FLAG, 0, head, 14, VERSION_FLAG.length);
		// 报文加密标识位b: 0表示报文不加密，1表示报文加密。占1字节
		// byte ENCRYPT_FLAG =
		// ((T809_MessageHeader)message.getMessageHeader()).getEncryptFlag();
		head[17] = 0x00;
		// 数据加密的密匙，长度为4个字节。
		byte[] ENCRYPT_KEY = BytesUtil.int2bytes4(((T809_MessageHeader) message.getMessageHeader()).getEncryptKey());

		System.arraycopy(ENCRYPT_KEY, 0, head, 18, 4);

		byte[] body = new byte[28];
		//车牌号
		byte[] vehicle_No = null;
		try {
			vehicle_No = body1206.getVEHICLE_NO().getBytes("GBK");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		System.arraycopy(vehicle_No, 0, body, 0, vehicle_No.length);

		byte vehicle_Color = body1206.getVEHICLE_COLOR();

		body[21] = vehicle_Color;

		// byte[] data_Type = BytesUtil.int2bytes2(body9208.getDATA_TYPE());
		//
		// System.arraycopy(data_Type, 0, body, 22, data_Type.length);
		body[22] = (byte) 0x12;
		body[23] = 0x06;
		//数据长度
		byte[] data_Length = BytesUtil.int2bytes4(0);
		data_Length = BytesUtil.getBigDWord(0, data_Length);
		System.arraycopy(data_Length, 0, body, 24, data_Length.length);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] mes = null;
		try {
			bos.write(0x00);
			// 将数据头与数据体写入流中
			bos.write(head);
			bos.write(body);
			bos.write(0x00);
			bos.write(0x00);
			bos.write(0x00);
			// 将数据流转成byte数组
			byte[] bosBytes = bos.toByteArray();
			// 计算crc校验码
			byte[] CRCCode = T809_Util.check(bosBytes);
			bosBytes[bosBytes.length - 3] = CRCCode[0];
			bosBytes[bosBytes.length - 2] = CRCCode[1];
			// 将数据转义
			mes = T809_Util.escapeData(bosBytes);
			mes[0] = 0x5b;
			// 加入数据头与数据尾
			mes[mes.length - 1] = 0x5d;
		} catch (IOException e) {
			log.warn("", e);
		} finally {
			// 如果数据流不为空，关闭流
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.warn("", e);
				}
			}
		}

		log.debug("结束车辆定位信息交换应答消息" + BytesUtil.bytesToHexString(mes));
		return mes;

	}

}
